#!/usr/bin/env groovy

def DEPLOY_IMAGE
def current_image_tag

pipeline {
    agent { label "${BUILD_NODE}" }
    options {
        timestamps()
        timeout(15)
    }
    stages {
        stage('Build') {
            steps {
                echo "$WORKSPACE"
                sh "${NODEJS_BUILD_ARGS}"
            }
        }
        
        stage('制作镜像') {
            steps {
                script {
                    echo "正在发布分支：${mbranch}"
                    if (("${mbranch}" =~ '.*quality').matches()) {
                        current_image_tag = 'quality'
                    }else if (("${mbranch}" =~ '.*material').matches()) {
                        current_image_tag = 'material'
                    }else if (("${mbranch}" =~ '.*safety').matches()) {
                        current_image_tag = 'safety'
                    }else if (("${mbranch}" =~ '.*sys').matches()) {
                        current_image_tag = 'sys'
                    }else if (("${mbranch}" =~ '.*project').matches()) {
                        current_image_tag = 'project'
                    }else if (("${mbranch}" =~ '.*build').matches()) {
                        current_image_tag = 'build'
                    }
                    if ("${current_image_tag}" != null) {
                        DEPLOY_IMAGE = "${LOCAL_DOCKER_IMAGES_ADDR}/cscec_${ENV}/${PROJECT_NAME}:${current_image_tag}"
                    }else {
                        DEPLOY_IMAGE = "${LOCAL_DOCKER_IMAGES_ADDR}/cscec_${ENV}/${PROJECT_NAME}"
                    }
                    
                }
                echo 'Deploying'
                dir("$WORKSPACE"){
                    sh """
cd dist && \
cat >Dockerfile <<EOF
FROM nginx
#RUN mkdir -p /home/app/
COPY . /usr/share/nginx/html
VOLUME /etc/nginx/conf.d
EXPOSE 80
EOF
"""
                    sh """
                        cd dist && \
                        docker rmi -f "${DEPLOY_IMAGE}" || true \
                        && docker build -t "${DEPLOY_IMAGE}" . \
                        && docker push "${DEPLOY_IMAGE}"
                    """
                }
            }
        }
        stage('部署到业务集群') {
            steps {
                script {
                    if ("${ENV}" == 'test' && "${current_image_tag}" == 'material') {
                        echo '正在发布到测试集群'
                        echo "${TEST_MATERIAL_RANCHER_API}"
                        rancherRedeploy alwaysPull: true, credential: "${RANCHER_TOKEN}", images: "${DEPLOY_IMAGE}", workload: "${TEST_MATERIAL_RANCHER_API}"
                    }else if ("${ENV}" == 'test' && "${current_image_tag}" == 'project'){
                        echo '正在发布到测试集群'
                        echo "${TEST_PROJECT_RANCHER_API}"
                        rancherRedeploy alwaysPull: true, credential: "${RANCHER_TOKEN}", images: "${DEPLOY_IMAGE}", workload: "${TEST_PROJECT_RANCHER_API}"
                    }else if ("${ENV}" == 'test' && "${current_image_tag}" == 'quality'){
                        echo '正在发布到测试集群'
                        echo "${TEST_QUALITY_RANCHER_API}"
                        rancherRedeploy alwaysPull: true, credential: "${RANCHER_TOKEN}", images: "${DEPLOY_IMAGE}", workload: "${TEST_QUALITY_RANCHER_API}"
                    }else if ("${ENV}" == 'test' && "${current_image_tag}" == 'safety'){
                        echo '正在发布到测试集群'
                        echo "${TEST_SAFETY_RANCHER_API}"
                        rancherRedeploy alwaysPull: true, credential: "${RANCHER_TOKEN}", images: "${DEPLOY_IMAGE}", workload: "${TEST_SAFETY_RANCHER_API}"
                    }else if ("${ENV}" == 'test' && "${current_image_tag}" == 'sys'){
                        echo '正在发布到测试集群'
                        echo "${TEST_SYS_RANCHER_API}"
                        rancherRedeploy alwaysPull: true, credential: "${RANCHER_TOKEN}", images: "${DEPLOY_IMAGE}", workload: "${TEST_SYS_RANCHER_API}"
                    }else if ("${ENV}" == 'dev') {
                        echo '正在发布到开发集群'
                        echo "${DEV_RANCHER_API}"
                        rancherRedeploy alwaysPull: true, credential: "${RANCHER_TOKEN}", images: "${DEPLOY_IMAGE}", workload: "${DEV_RANCHER_API}"
                    }else if ("${ENV}" == 'test'){
                        echo '正在发布到测试集群'
                        echo "${TEST_SYS_RANCHER_API}"
                        rancherRedeploy alwaysPull: true, credential: "${RANCHER_TOKEN}", images: "${DEPLOY_IMAGE}", workload: "${TEST_SYS_RANCHER_API}"
                    }
                }
            }
        }
    }
}
